package cn.hnkjxy.web.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.hnkjxy.po.Order;
import cn.hnkjxy.po.Product;
import cn.hnkjxy.po.User;
import cn.hnkjxy.service.OrderService;

@Controller
public class OrderController {
	@Autowired
	private OrderService orderService;

	@RequestMapping(value = "/findUserOrder")
	public String findUserOrder(Model model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		List<Order> orders = this.orderService.findUserOrder(user.getUsid());
		model.addAttribute("orders", orders);
		return "query";
	}

	@RequestMapping(value = "/payDtat", method = RequestMethod.POST)
	public String addOrder(HttpSession session, Order order) {
		Product product = (Product) session.getAttribute("product");
		User user = (User) session.getAttribute("user");
		order.setProduct(product);
		order.setUsid(user.getUsid());
		session.setAttribute("order", order);
		return "pay";
	}

	@RequestMapping(value = "/pay", method = RequestMethod.POST)
	public String pay(HttpSession session) {
		Order order = (Order) session.getAttribute("order");
		Integer row = this.orderService.addOrder(order);
		return "redirect:modifyproductstatus";
	}

	@RequestMapping(value = "/deleteOrder")
	@ResponseBody
	public String deleteOrder(Integer id) {
		Integer row=this.orderService.deleteOrder(id);
		if(row>0) {
			return "ok";
		}
		else {
			return "no";
		}
	}
	
	@RequestMapping(value="findByOrder",method=RequestMethod.POST)
	public String findByOrder(HttpSession session,String name,String tel,Integer orid,Integer usid) {
		User user = (User) session.getAttribute("user");
		usid= user.getUsid();
		List<Order> orders = this.orderService.findByOrder(name,tel,orid,usid);
		session.setAttribute("orders", orders);
		return "query";
	}
	
	@RequestMapping(value="/admin/findAllOrder",method=RequestMethod.GET)
	public String findAllOrder(HttpSession session) {
		List<Order> orders=this.orderService.findAllOrder();
		session.setAttribute("orders", orders);
		return "admin/order";
	}
	
	@RequestMapping(value="/admin/findByAdminOrder")
	public String findByAdminOrder(HttpSession session,String name,String number,Integer prid) {
		List<Order> orders=this.orderService.findByAdminOrder(name, number, prid);
		session.setAttribute("orders", orders);
		return "admin/order";
	}
	
	@RequestMapping(value="admin/deleteByAdminOrder")
	@ResponseBody
	public String deleteByAdminOrder(Integer id) {
		Integer row= this.orderService.deleteByAdminOrder(id);
		if(row>0) {
			return "ok";
		}else {
			return "no";
		}
		
	}
	
	@RequestMapping(value="/admin/findOrderData")
	@ResponseBody
	public String findOrderData(HttpSession session) {
		List<Order> orders= this.orderService.findOrderData();
		return JSON.toJSONString(orders);
	}
	
	@RequestMapping(value="/admin/Data")
	public String Data() {
		return "admin/data";
	}
}
